package study.core.rdd.operator

import org.apache.spark.rdd.RDD
import org.apache.spark.{HashPartitioner, SparkConf, SparkContext}

/**
 * 转换算子之键值操作reduceByKey操作
 * 将数据按照相同的 Key 对 Value 进行聚合
 *
 * @author zh
 * @date 2021/5/15 10:47
 */
object TestReduceByKey {
  def main(args: Array[String]): Unit = {
    val sparkConf = new SparkConf().setMaster("local[*]").setAppName("rddMap")
    val context = new SparkContext(sparkConf)

    // 准备集合
    val list = List(("a",1),("b",2),("a",3),("b",4),("a",5))
    // 创建RDD
    val rdd = context.makeRDD(list, 2)

    // 聚合
    val result = rdd.reduceByKey(_+_)
    result.collect().foreach(println)

    context.stop()

    //(b,6)
    //(a,9)

  }

}
